package com.sample.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.sample.action.form.UserForm;
import com.sample.javabean.Area;
import com.sample.javabean.Institution;
import com.sample.javabean.Role;
import com.sample.javabean.User;
import com.sample.util.Pager;

/**
 * 用户管理模块
 * @author 演示
 *
 */
public class UserAction extends BaseAction{
	private final static Log log=LogFactory.getLog(UserAction.class);
	private UserForm userForm=new UserForm();
	private User user=new User();
	private String user_id;
	private String yzcode;
	private Pager<User> pageList;
	private List<Institution> insList;
	private List<Role> roleList;
	
   //构造函数
	public UserAction() {
		super();
	}
	public UserAction(UserForm userForm, User user, String user_id,
			Pager<User> pageList) {
		super();
		this.userForm = userForm;
		this.user = user;
		this.user_id = user_id;
		this.pageList = pageList;
	}
	/**
	 * 添加用户
	 * @return
	 * @throws Exception
	 */
	public String add()throws Exception{
		try{
			if(userForm!=null){
				
				user.addUser(userForm);	
			}
			return SUCCESS;
		}catch (Exception e) {
			System.out.println("user_id = "+userForm.getUser_id()+",role_name = "+userForm.getRole_name()
					+",full_name"+userForm.getFull_name()+",pwd = "+userForm.getPwd()+",ins_name = "+userForm.getIns_name());
			throw new Exception("添加失败");
			
		}
		
	}
	/**
	 * 用户登录
	 * @return
	 * @throws Exception
	 */
	public String login()throws Exception{
		try{
			log.info("用户登录:"+yzcode);
			 //获取验证码
			String key_yzm = (String)getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
			boolean flag=false;
			if(userForm!=null){
				user.setUser_id(userForm.getUser_id());
				user.setPwd(userForm.getPwd());
                user.load();
				
				ActionContext actionContext = ActionContext.getContext();
				Map session = actionContext.getSession();
				session.put("user",user);
				
				user_id= user.getUser_id();
				flag =user.check();
			}
			String targetUrl=flag?"login":"error";
			return targetUrl;
		}catch (Exception e) {
			System.out.println("user_id="+user.getUser_id());
			throw new Exception("用户名或者密码有误:"+e.getMessage());
		}
		
	}
	/**
	 * 用户退出
	 * @return
	 * @throws Exception
	 */
	public String logOut()throws Exception{
		try{
			log.info("退出系统:");
			//会话强制失效
			getSession().invalidate();
			return "logout";
		}catch (Exception e) {
			e.printStackTrace();
			throw new Exception("用户名或者密码有误:"+e.getMessage());
		}
		
	}
	/**
	 * 显示用户记录列表
	 * @return
	 * @throws Exception
	 */
	public String listUser()throws Exception{
		try{
			pageList=user.list(page, size, userForm);
			return "listUser";
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
		
	}
	
	public String inputUser()throws Exception{
		try{
			Institution ins = new Institution();
			//加载网点信息
			insList =ins.findAll();
			Role role = new Role();
			roleList = role.findAll();
			return "inputUser";
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		
		
	}
	public String editUser()throws Exception{
		try{
			user.setUser_id(user_id);
			user.load();
			Institution ins = new Institution();
			//加载经办点信息
			insList =ins.findAll();
			Role role = new Role();
			roleList = role.findAll();
			return "editUser";
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}	
	}
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit()throws Exception{
		try{
			if(userForm!=null){
				user.editUser(userForm);	
			}
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}	
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String del()throws Exception{
		try{
			if(userForm!=null){
				user.setUser_id(user_id);
				user.delUser();	
			}
			return SUCCESS;
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}	
	}
	//属性访问方法
	public UserForm getUserForm() {
		return userForm;
	}
	public void setUserForm(UserForm userForm) {
		this.userForm = userForm;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	public Pager<User> getPageList() {
		return pageList;
	}
	public void setPageList(Pager<User> pageList) {
		this.pageList = pageList;
	}
	public static Log getLog() {
		return log;
	}
	public String getYzcode() {
		return yzcode;
	}
	public void setYzcode(String yzcode) {
		this.yzcode = yzcode;
	}
	public List<Institution> getInsList() {
		return insList;
	}
	public void setInsList(List<Institution> insList) {
		this.insList = insList;
	}
	public List<Role> getRoleList() {
		return roleList;
	}
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}


}
